<template>
  <t-tooltip :content="help" :trigger="cpTooltipTrigger">
    <t-date-picker
      v-model="valueThis"
      @change="change"
      v-bind="$attrs"
      :clearable="getPropValue('clearable', true)"
      :enableTimePicker="getPropValue('enableTimePicker', false)"
      @focus="_focus"
      :readonly="getPropValue('readonly', false)"
      :disabled="getPropValue('disabled', false)"
      :allowInput="getPropValue('allowInput', true)"
      :mode="getPropValue('mode', 'date')"
      :popupProps="getPropValue('popupProps', {})"
      ref="controlInst"
      :class="{'custom-date-picker': true, 'custom-date-picker-inner-cell': isInnerCell, 'custom-date-picker-With-hms': isWithHms, 'custom-date-picker-With-hms-form': getPropValue('isWithHmsForm', false)}"
    >
    </t-date-picker>
  </t-tooltip>
</template>

<script>

export default {
  desc: '日期控件',
  components: {
  },
  name: 'customDatePicker',
  props: {
    /**
     * @Description：上级页面对象
     * @Date 2023-10-10
     */
    parent: {
      type: Object,
      default: () => {}
    },
    /**
     * @Description：上级表单对象
     * @Date 2023-10-10
     */
    form: {
      type: Object,
      default: () => {}
    },
    /**
     * @Description：输入提示信息
     * @Date 2023-10-10
     */
    help: {
      type: String,
      default: () => ''
    },
    /**
     * @Description：是否是表单项内部
     * @Date 2023-10-10
     */
    isInnerForm: {
      type: Boolean,
      default: () => true
    },
    /**
     * @Description：是否在单元格中
     * @Date 2023-10-10
     */
    isInnerCell: {
      type: Boolean,
      default: () => false
    },
    /**
     * @Description：带时分秒
     * @Date 2023-10-10
     */
    isWithHms: {
      type: Boolean,
      default: () => false
    }
  },
  created() {
    this.bindFormItemProp()
  },
  mounted() {
    setTimeout(() => {
      $(this.$refs.controlInst.$el).find('input').bind("input propertychange", this.oninput)
    }, 500)
  },
  data() {
    return {
      valueThis: null,

      // 表单配置
      formItemProp: {
        name: '',
        label: ''
      }
    }
  },
  methods: {
    // 值变化更新表单值
    change() {
      this.parent.$set(this.form, this.formItemProp.name, this.valueThis)
      this.$emit('change')
    },
    // 值变化更新表单值
    oninput() {
      let inputValue = this.$refs.controlInst.inputValue
      let dateStr = this.dates.getDateYMD(inputValue)
      if (dateStr != null) {
        this.valueThis = dateStr
        this.change()
      }
    },

    /**
     * @Description：获取属性值
     * @Author wyt
     * @Date 2023-09-27
     */
    getPropValue(name, defaultValue) {
      if (this.$attrs && this.$attrs[name] != undefined) {
        return this.$attrs[name]
      }
      return defaultValue
    },
    /**
     * @Description：获取表单配置
     * @Author wyt
     * @Date 2023-09-27
     */
    bindFormItemProp() {
      if (this.isInnerForm) {
        if (!this.$parent.$options || !this.$parent.$options.propsData) {
          console.error('字段未找到表单项，请检查')
          return false
        }
        this.formItemProp = {
          name: this.$parent.$options.propsData.name,
          label: this.$parent.$options.propsData.label
        }
      } else {
        this.formItemProp = {
          name: this.$attrs.name,
          label: this.$attrs.label
        }
      }

      if (!this.formItemProp.name) {
        return false
      }
      if (!this.formItemProp.label) {
        return false
      }
    },
    /**
     * @Description：聚焦事件,回调上级
     * @Author wyt
     * @Date 2023-09-27
     */
    _focus() {
      this.$emit('focus')
    },
    /**
     * @Description：聚焦事件，使当前组件聚焦
     * @Author wyt
     * @Date 2023-09-27
     */
    focus() {
      this.$refs.controlInst.focus()
    }
  },
  watch: {
    // 监听表单设置本组件的值
    form: {
      immediate: true, // 初始创建 watch 时立即执行回调函数
      deep: true, // 监听到对象里面的值的变化
      handler() {
        this.valueThis = null
        if (this.form) {
          this.bindFormItemProp()
          this.valueThis = this.form[this.formItemProp.name]
        }
      },
    }
  },
  computed: {
    cpTooltipTrigger() {
      return this.strings.isBlank(this.help) ? '' : 'hover'
    }
  }
}
</script>

<style lang="scss" >
/*内置表格中*/
.custom-date-picker-inner-cell{
 .t-input{
   width: 73% !important;
   font-size: 12px !important;
   line-height: 16px !important;
   height: 25px !important;
   margin-top: 5px !important;
 }
}
/*内置表格中*/
.custom-date-picker-With-hms{
 .t-input{
   width: 100% !important;
 }
}
/*内置表格中*/
.custom-date-picker-With-hms-form{
 .t-input{
   width: 100% !important;
 }
  .t-input__suffix{
    position: absolute;
    right: -2px;
  }
}
</style>
